﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls;
using StudentManage.Data;
using StudentManage.Common;

namespace StudentManage.Web
{
    public class SecurityPage : Page
    {
        public UserList CurrentUser { get; set; }

        public virtual string PrivCode()
        {
            return "Default";
        }

        public string PrivilegeCode { get { return PrivCode(); } }
        public List<string> userAllRolePriv { get; set; }

        public override void Dispose()
        {
            base.Dispose();
            //LinqUtility.LinqContext.Dispose();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (this.DesignMode) return;

            LinqUtility.LinqContext = new StudentManageDataContext();

            if (User.Identity.IsAuthenticated)
            {

                CurrentUser = LinqUtility.LinqContext.UserList.FirstOrDefault(p => p.UserName == User.Identity.Name);
            }

            userAllRolePriv = (from a in LinqUtility.LinqContext.RoleUsers
                               join b in LinqUtility.LinqContext.RolePrivileges
                                   on a.RoleCode equals b.RoleCode
                               where a.UserCode == User.Identity.Name
                               select b.PrivCode).ToList();
            userAllRolePriv.Add("Default");

            CheckEnablePage(userAllRolePriv);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            Page.Controls.AllOfType<Label>().ToList().ForEach(p =>
            {
                if (p.ID == "master_UserName")
                {
                    p.Text = CurrentUser.UserName;
                }
            });
        }

        protected override void Render(HtmlTextWriter writer)
        {
            
            base.Render(writer);
            if (PrivilegeCode.IsNullOrEmpty()) return;

            CheckEnableControl(userAllRolePriv);
        }

        private void CheckEnablePage(List<string> userAllRolePriv)
        {
            bool hasPrivilege = userAllRolePriv.Contains(PrivilegeCode);
            if (!hasPrivilege && User.Identity.IsAuthenticated)
            {
                //Response.Close();
                //Page.Dispose();
                Response.Redirect("~/NoPower.aspx");
                //Page.AlertAndClose("您没有使用此页面的权限！请联系管理员。");
            }
        }

        private void CheckEnableControl(List<string> rolePrivs)
        {
            this.Controls.AllOfType<Button>().ToList().ForEach(p =>
              {
                  string PrivCode = p.Attributes["PrivCode"];
                  if (!PrivCode.IsNullOrEmpty())
                  {
                      p.Visible = rolePrivs.Contains(PrivCode);
                  }
              });

            this.Controls.AllOfType<ImageButton>().ToList().ForEach(p =>
            {
                string PrivCode = p.Attributes["PrivCode"];
                if (!PrivCode.IsNullOrEmpty())
                {
                    p.Visible = rolePrivs.Contains(PrivCode);
                }
            });
        }
  
    }
}